﻿using Sherwood.Content.DO.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Sherwood.Content.Tasks;
using System.Collections.Generic;
using System.Linq;

namespace Sherwood.Content.DO.Tests
{
    
    
    /// <summary>
    ///This is a test class for TaskManagerTest and is intended
    ///to contain all TaskManagerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class TaskManagerTest : AutoBuildTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for GetNextTaskExecution
        ///</summary>
        [TestMethod()]
        public void GetNextTaskExecutionTest()
        {
            DateTime expected = DateTime.UtcNow.AddSeconds(5);

            TaskManager target = new TaskManager();

            var task = new TestTask(expected);
            var task2 = new TestTask(expected.AddSeconds(10));
            
            DateTime actual;
            actual = target.GetNextTaskExecution();
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for GetExecutableTasks
        ///</summary>
        [TestMethod()]
        public void GetExecutableTasksTest()
        {
            var task = new TestTask(DateTime.UtcNow.AddSeconds(-5));
            var task2 = new TestTask(DateTime.UtcNow.AddSeconds(-10));
            var task3 = new TestTask(DateTime.UtcNow.AddSeconds(5));

            TaskManager target = new TaskManager(); 
            IEnumerable<ITask> actual;
            actual = target.GetExecutableTasks();
            Assert.AreEqual(2, actual.Count());
            Assert.IsTrue(actual.Contains(task));
            Assert.IsTrue(actual.Contains(task2));
            Assert.IsFalse(actual.Contains(task3));
        }

        /// <summary>
        ///A test for CreateNew
        ///</summary>
        public void CreateNewTestHelper<TTask>()
            where TTask : class , ITask
        {
            TaskManager target = new TaskManager(); 
            TTask actual;
            var dateTime = DateTime.UtcNow.AddSeconds(10);
            actual = target.CreateNew<TTask>(dateTime);
            var second = target.CreateNew<TTask>(actual);
            Assert.AreEqual(dateTime, actual.PlannedDate);
            Assert.AreEqual(dateTime, second.PlannedDate);
        }

        [TestMethod()]
        public void CreateNewTest()
        {
            CreateNewTestHelper<TestTask>();
        }
    }
}
